package com.jinglvhuanjing.app.base;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jinglvhuanjing.app.R;
import com.jinglvhuanjing.app.bean.CityBean;
import com.jinglvhuanjing.app.helper.statusbar.StatusBarHelper;
import com.jinglvhuanjing.app.listener.HttpListener;
import com.jinglvhuanjing.app.utils.AppManager;
import com.jinglvhuanjing.app.utils.Constants;
import com.jinglvhuanjing.app.utils.SpUtils;
import com.jinglvhuanjing.app.widget.LeftDataView;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public abstract class BaseActivity extends AppCompatActivity {

    protected String TAG = getClass().getSimpleName();

    protected Handler mHandlerLeft;
    protected MyRunnable mRunnableLeft;

    /**
     * 在此方法创建你的UI视图，此视图将会被添加到根视图中
     *
     * @return {@link LayoutRes}
     */
    @LayoutRes
    protected abstract int getLayoutId();

    protected abstract void initView(Bundle savedInstanceState);

    private StatusBarHelper mStatusBarHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isManagerActivity()) {
            AppManager.newInstance().addActivity(this);
        }
        this.mStatusBarHelper = new StatusBarHelper(this);
        this.mStatusBarHelper.setScreenFullNavigationBar(this);
        this.mStatusBarHelper.setStatusBarThemeDark(this, true);

        setContentView(getLayoutId());
        this.initView(savedInstanceState);

        this.mHandlerLeft = new Handler(Looper.myLooper());
        this.mRunnableLeft = new MyRunnable(this);
        this.mHandlerLeft.post(this.mRunnableLeft);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume");
//        DeviceUtil.hideNavigation(getApplicationContext());
    }

    /**
     * 是否需要将Activity添加到AppManager中统一管理，默认添加管理。
     */
    public boolean isManagerActivity() {
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isManagerActivity()) {
            AppManager.newInstance().finish(this);
        }
        this.mHandlerLeft.removeCallbacks(this.mRunnableLeft);
        this.mRunnableLeft = null;
    }

    protected void setLeftViewTime() {
        LeftDataView leftDataView = findViewById(R.id.left_content_layout);
        if (leftDataView != null) {
            leftDataView.setTime();
        }
    }

    protected void setLeftView() {
        LeftDataView leftDataView = findViewById(R.id.left_content_layout);
        if (leftDataView != null) {
            leftDataView.setTitle(SpUtils.getString(getApplicationContext(), Constants.LEFT_TITLE));
            leftDataView.setQrcodeSecond(SpUtils.getString(getApplicationContext(), Constants.DEVICE_ID));
            CityBean cityBean = SpUtils.getObject(getApplicationContext(), Constants.CITY_BEAN);
            if (cityBean != null) {
                setLeftViewTianqi(cityBean);
            }
        }
    }

    protected void setLeftViewTianqi(CityBean cityBean) {
        LeftDataView leftDataView = findViewById(R.id.left_content_layout);
        if (leftDataView != null) {
            leftDataView.setTianQi(cityBean);
        }
    }

    private static class MyRunnable implements Runnable {
        private WeakReference<BaseActivity> weakReference;

        private MyRunnable(BaseActivity activity) {
            weakReference = new WeakReference<>(activity);
        }

        @Override
        public void run() {
            BaseActivity activity = weakReference.get();
            if (activity == null) {
                return;
            }
            activity.setLeftViewTime();
            activity.mHandlerLeft.postDelayed(this, 1000);
        }
    }

    /**
     * 设置连接超时时间
     */
    public final static int CONNECT_TIMEOUT = 60;
    /**
     * 设置读取超时时间
     */
    public final static int READ_TIMEOUT = 100;
    /**
     * 设置写的超时时间
     */
    public final static int WRITE_TIMEOUT = 60;

    protected OkHttpClient getOkHttpClient() {
        OkHttpClient mClient = new OkHttpClient.Builder()
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)//设置写的超时时间
                .build();
        return mClient;
    }

    protected void requestGetTianqi(String cityId, HttpListener listener) {
        final Request request = new Request.Builder()
                .url(Constants.TIANQI_URL + cityId)
                .get()
                .build();
        OkHttpClient mClient = getOkHttpClient();
        Call call = mClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (listener != null) {
                    listener.onFail(e.toString());
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                listener.onSuccess(response.body().string());
            }
        });
    }
}
